﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RPGProject.IOBuffers
{
    class StatOutputBuffer : OutputBuffer
    {
        private Screen parent;
        /// <summary>
        /// A list of stats and their incode names.
        /// </summary>
        private Dictionary<String, Stat> stats;
        /// <summary>
        /// Creates a StatOutputBuffer, which basically updates data on the screen.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="parent"></param>
        public StatOutputBuffer(int width, int height, Screen parent = null)
            : base(width, height, new CharInfo(' '), false)
        {
            this.parent = parent;
            this.stats = new Dictionary<String, Stat>();
        }
        /// <summary>
        /// The Delegate function to be sent to other classes to grab info.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void OnPropUpdate(String name, Object value)
        {
            if(this.stats.ContainsKey(name))
            {
                Stat current = this.stats[name];
                current.value = value;
                this.WriteStat(current);
            }
        }
        /// <summary>
        /// Adds a Stat to the list, with the incode name as specified.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="stat"></param>
        public void AddStat(String name, Stat stat)
        {
            if (stat.lineNumber > this.height)
                return;
            if (stat.x + stat.width > this.width)
                return;
            if (stat.x < 0)
                return;
            this.stats.Add(name, stat);
        }
        /// <summary>
        /// Prints the stat out to the buffer.
        /// </summary>
        /// <param name="current"></param>
        private void WriteStat(Stat current)
        {
            String toPrint = current.name + ": " + ((current.value == null) ? "null" : current.value.ToString());
            for (int i = 0; i < current.width; i++)
            {
                if (i >= toPrint.Count())
                    this[i + current.x, current.lineNumber] = new CharInfo(' ');
                else
                    this[i + current.x, current.lineNumber] = new CharInfo(toPrint[i]);
            }
        }
        /// <summary>
        /// A class for holding data about Stats.
        /// </summary>
        public class Stat
        {
            public String name;
            public Object value;
            public int lineNumber;
            public int x;
            public int width;
            public Stat(String name, int lineNumber, int x, int width, Object value = null)
            {
                this.name = name;
                this.lineNumber = lineNumber;
                this.x = x;
                this.width = width;
            }
        }
    }
}
